package com.cxf.windex.rest.minab;

import com.cxf.windex.base.msg.ObjectRestResponse;
import com.cxf.windex.base.msg.TableResultResponse;
import com.cxf.windex.base.rest.BaseRestController;
import com.cxf.windex.biz.BMeterialsBiz;
import com.cxf.windex.entity.BMeterials;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @program: business_needs
 * @description: 素材
 * @author: chenxf
 * @create: 2018-10-16 15:44
 **/
@Api(tags = {"村广通[小程序-商] - 素材接口"})
@Controller
@RequestMapping("userb/meterials")
public class BMeterialsController extends BaseRestController<BMeterialsBiz,BMeterials> {
  @ApiOperation(value = "查询素材", notes = "查询店铺素材")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "limit", value = "记录数 默认10", required = false, paramType = "query", dataType = "int"),
      @ApiImplicitParam(name = "offset", value = "页码 默认 1", required = false, paramType = "query", dataType = "int"),
      @ApiImplicitParam(name = "orderStr", value = "排序 默认desc", required = false, paramType = "query", dataType = "String"),
      @ApiImplicitParam(name = "type", value = "素材类型 0海报 1语音", required = true, paramType = "query", dataType = "string"),
      @ApiImplicitParam(name = "shopId", value = "店铺ID", required = true, paramType = "query", dataType = "long")
  })
  @RequestMapping(value = "/list", method = RequestMethod.GET)
  @ResponseBody
  public TableResultResponse<BMeterials> list(
      @RequestParam(defaultValue = "10") int limit, @RequestParam(defaultValue = "1") int offset,
      @RequestParam(defaultValue = "desc") String orderStr,String type,Long shopId) {
    List<BMeterials> items = baseBiz.list(limit, offset, orderStr,type,shopId);
    PageInfo<BMeterials> pageInfo = new PageInfo<>(items);
    return new TableResultResponse<BMeterials>(Integer.parseInt(String.valueOf(pageInfo.getTotal())),
        offset, limit,
        items);
  }

  @ApiOperation(value = "获取素材信息", notes = "根据ID获取素材信息")
  @ApiImplicitParams({
   @ApiImplicitParam(name = "id", value = "素材ID", required = true, paramType = "query", dataType = "string")
  })
  @RequestMapping(value = "/getInfo", method = RequestMethod.GET)
  @ResponseBody
  public ObjectRestResponse getInfo(String id){
    BMeterials entity=baseBiz.selectById(id);
    if(entity==null){
      return new ObjectRestResponse("素材不存在").rel(false);
    }
    return new ObjectRestResponse("查询成功").rel(true).result(entity);
  }

  @ApiOperation(value = "保存素材", notes = "保存素材")
  @RequestMapping(value = "/save", method = RequestMethod.POST)
  @ResponseBody
  public ObjectRestResponse save(BMeterials entity){
    return new ObjectRestResponse("保存成功").rel(true).result(baseBiz.save(entity));
  }
}
